package com.demo.multThread;

import com.demo.multThread.BorderInit;
import com.demo.io.RecieveScreen;
import com.demo.io.SendScreen;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class App extends JFrame {

    // 定义组件
    private static final long serialVersionUID = 1L;
    private JTabbedPane jTabbedPane;
    private JPanel contentPane;
    private JTextField tfName;
    private JTextField tfNum;
    private JTextField allInfo;
    private JTextField tfName2;
    private JTextField JHostName;
    private JTextField tfName1, JIP1_1, JPort1_1;
    private JRadioButton rb1, rb2;
    private JCheckBox cb1, cb2, cb3;
    private JComboBox<String> t1, t2, t3;
    private int DEFAULT_PORT=19000;
    private Server server;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    App frame = new App();     // 创建一个窗口
                    frame.setVisible(true);    // 让该窗口实例可见
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 窗口属性的设置，内部组件的初始化
     */
    public App() {
        setTitle("SPDB Remote Desktop");                           // 标题
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     // 设置关闭时推出JVM
        setSize(500, 300);                                   // 设置窗口大小
        setLocationRelativeTo(null);                        // 设置窗体居中

        jTabbedPane = new JTabbedPane();

        contentPane = new JPanel();                           // 内容面板
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));      // 设置布局
        setContentPane(contentPane);
        JPanel panel = new JPanel(new GridLayout(3, 1, 5, 3));
        panel.setBorder(new EmptyBorder(20, 5, 5, 5));
        contentPane.add(panel, BorderLayout.CENTER);          // 给panel添加边框
        contentPane.add(jTabbedPane);
        jTabbedPane.add("连接", panel);
        // 第一行
        JPanel panel_1 = new JPanel(new GridLayout(1, 2, 1, 1));
        panel.add(panel_1);

        JPanel panel1_1_1 = new JPanel(new GridLayout(2, 2, 1, 1));
        panel_1.add(panel1_1_1);
        JLabel lable_ip = new JLabel("IP", SwingConstants.CENTER);
        panel1_1_1.add(lable_ip);

        JLabel lable_port = new JLabel("PORT", SwingConstants.CENTER);
        panel1_1_1.add(lable_port);

        JIP1_1 = new JTextField();
        panel1_1_1.add(JIP1_1);
        JIP1_1.setColumns(20);

        JPort1_1 = new JTextField();
        panel1_1_1.add(JPort1_1);
        JPort1_1.setColumns(20);
        JPort1_1.setText(Integer.toString(DEFAULT_PORT));

        // 第一行 右侧
        JPanel panel1_1_2 = new JPanel(new GridLayout(2, 1, 1, 1));
        panel_1.add(panel1_1_2);
        JLabel label1_2 = new JLabel("PassWord", SwingConstants.CENTER);
        panel1_1_2.add(label1_2);
        JTextField JPwd1 = new JTextField();
        panel1_1_2.add(JPwd1);
        JPwd1.setColumns(20);

        JPanel panel1_2 = new JPanel();
        panel.add(panel1_2);
        JLabel warining1_1 = new JLabel("IP 不可为空");
        warining1_1.setVisible(false);
        warining1_1.setForeground(Color.RED);
        panel1_2.add(warining1_1);

        // 第二行
        JPanel panel_2 = new JPanel();
        panel.add(panel_2);
        JButton conn = new JButton("连接");
        panel_2.add(conn);
        JButton jbRest = new JButton("重填");
        panel_2.add(jbRest);

        /*******************************************************************************/
        JPanel panel2 = new JPanel(new GridLayout(3, 1, 5, 3));  // 表格布局
        panel2.setBorder(new EmptyBorder(20, 5, 5, 5));
        contentPane.add(jTabbedPane);
        jTabbedPane.add("共享", panel2);
        // 第一行 左侧
        JPanel panel2_1 = new JPanel(new GridLayout(1, 2, 5, 3));
        panel2.add(panel2_1);

        JPanel panel2_1_1 = new JPanel(new GridLayout(2, 1, 1, 1));
        panel2_1.add(panel2_1_1);
        JLabel label2 = new JLabel("IP", SwingConstants.CENTER);
        panel2_1_1.add(label2);
        tfName2 = new JTextField();
        tfName2.setEditable(false);
        panel2_1_1.add(tfName2);
        tfName2.setColumns(20);

        // 第一行 右侧
        JPanel panel2_1_2 = new JPanel(new GridLayout(2, 1, 1, 1));
        panel2_1.add(panel2_1_2);
        JLabel label2_2 = new JLabel("HostName", SwingConstants.CENTER);
        panel2_1_2.add(label2_2);
        JHostName = new JTextField();
        JHostName.setEditable(false);
        panel2_1_2.add(JHostName);
        JHostName.setColumns(20);

        // 第二行
        JPanel panel2_2 = new JPanel(new GridLayout(1, 2, 1, 1));
        panel2_2.setBorder(new EmptyBorder(10, 0, 5, 0));
        panel2.add(panel2_2);

        JPanel panel2_2_1 = new JPanel();
        panel2_2.add(panel2_2_1);
        JLabel label2_3 = new JLabel("HostName", SwingConstants.LEFT);
        panel2_2_1.add(label2_3);
        JTextField JPort = new JTextField();
//        JPort.setEditable(false);
        panel2_2_1.add(JPort);
        JPort.setColumns(5);
        JPort.setText(Integer.toString(DEFAULT_PORT));


        JPanel panel2_2_2 = new JPanel();
        panel2_2.add(panel2_2_2);
        JLabel label2_4 = new JLabel("密码", SwingConstants.RIGHT);
        panel2_2_2.add(label2_4);
        JTextField JPwd2 = new JTextField();
        JPwd2.setColumns(7);
        panel2_2_2.add(JPwd2);

        JPanel panel2_3 = new JPanel();
        panel2.add(panel2_3);
        JButton share = new JButton("共享");
        panel2_3.add(share);

        //添加标题
        JPanel panelNorth = new JPanel();
        contentPane.add(panelNorth, BorderLayout.NORTH);
        JLabel labelTitle = new JLabel("远程桌面工具");
        labelTitle.setForeground(Color.DARK_GRAY);
        labelTitle.setFont(new Font("宋体", Font.BOLD, 20));
        panelNorth.add(labelTitle);

        // 给连接按钮添加事件处理代码
        conn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StringBuilder info = new StringBuilder();
                String ip = JIP1_1.getText();
                String port = JPort.getText();
                int iPort = Integer.parseInt(port);
                if (0 == ip.length()) {
                    warining1_1.setVisible(true);
                } else {
                    warining1_1.setVisible(false);
                    info.append(ip);
                    System.out.println(info.toString());

                    // Client client = new Client(ip, iPort);
                    new Thread(new getImg(ip, iPort)).start();
                }
            }
        });

        // 给重填按钮设置事件处理代码
        jbRest.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tfName.setText("");
            }
        });

        share.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InetAddress addr = null;
                String ip = null, hostname = null, port = null;
                int iPort = 9000;
                if (share.getText().equals("共享")) {
                    try {
                        addr = InetAddress.getLocalHost();
                        ip = addr.getHostAddress();
                        tfName2.setText(ip);
                        System.out.println("Local HostAddress: " + ip);
                        hostname = addr.getHostName();
                        JHostName.setText(hostname);
                        System.out.println("Local host name: " + hostname);
                        port = JPort.getText();
                        if (port.length() < 4) {
                            System.out.println("");
                        } else {
                            iPort = Integer.parseInt(port);
                        }
                        System.out.println("Port: " + iPort);

                        server = new Server(iPort, 1);
                        new Thread(new getConn(server.serverSocket)).start();

                        share.setText("停止共享");
                    } catch (UnknownHostException ex) {
                        ex.printStackTrace();
                        tfName2.setText(ip);
                        JHostName.setText(hostname);
                    }
                } else {
                    share.setText("共享");
                    try {
                        server.serverSocket.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    System.out.println("停止共享");
                }

            }
        });

    }

}
